package com.etc.servlet;

import com.etc.dao.BookDao;
import com.etc.entity.Book;
import com.etc.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author kalista
 * @Description
 * @Date 2020/12/11  15:09
 **/
@WebServlet("/index")
public class IndexServlet  extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //把数据放到了request域中
        req.setAttribute("message","测试");

        //对象
        User book = new User(1,"王聪聪","123456");
        req.setAttribute("user",book);

        BookDao bookDao = new BookDao();
        try {
            List<Book> books = bookDao.getBooks();
            req.setAttribute("books",books);


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BookDao bookDao = new BookDao();
        try {
            List<Book> books = bookDao.getBooks();
            req.setAttribute("books",books);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }
}
